/**
 * 团队市场页面
 * author wim
 */
import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../util/Toast.dart';
import 'package:flutter_aygsh/base/application.dart';
import 'package:flutter_aygsh/entity/team_list_entity.dart';
import 'package:flutter_aygsh/http/Api.dart';
import 'package:flutter_aygsh/http/HttpUtil.dart';
import 'package:flutter_aygsh/util/AppSaveInfoUtil.dart';

import '../../base/Constant.dart';

class TeamMarketPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TeamMarketView();
  }
}

class TeamMarketView extends State<TeamMarketPage>
    with SingleTickerProviderStateMixin {
  List<String> tabList = ["全部"];
  String token = "";
  TabController mController;
  String totalNum = "";
  String avatar = "";
  String name = "";
  String auth_code = "";
  int page = 1;
  List<TeamListDataData> list1 = List();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    mController = TabController(
      length: tabList.length,
      vsync: this,
    );
    // setState(() {
    //   auth_code = APPLIC.map['user_msg']['auth_code'];
    //   name = APPLIC.map['user_detail']['nickname'] ??
    //       APPLIC.map['user_msg']['phone'];
    //   avatar = APPLIC.map['user_detail']['avatar'] ?? "";
    // });
    getString(TOKEN).then((value) {
      token = value;
      getTeamList();
    });
  }

  getTeamList() async {
    FormData formData =
        new FormData.fromMap({"token": token, "p": page, "per": 300});
    Response<String> response =
        await HttpUtil().post(Api.URL_USER_TEAMLIST, data: formData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    TeamListEntity homeListEntity = new TeamListEntity.fromJson(userMap);
    if (code == 1 && mounted) {
      setState(() {
        list1.addAll(homeListEntity.data.datas);
        // list2.addAll(homeListEntity.data.datas2);
        // totalNum = homeListEntity.data.referrer_num.toString();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    // TODO: implement build
    return new Scaffold(
        appBar: AppBar(
          title: Text('我的团队', style: TextStyle(fontSize: 16.0)),
          elevation: 0,
          backgroundColor: Color.fromARGB(255, 255, 81, 18),
        ),
        body: Container(
          color: divideColor,
          width: double.infinity,
          child: new Column(
            children: <Widget>[
              // Container(
              //   width: double.infinity,
              //   height: 0,
              //   padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
              //   decoration: new BoxDecoration(
              //       gradient: const LinearGradient(
              //           colors: [Color.fromARGB(255, 255, 81, 18), mainColor],
              //           begin: Alignment.topCenter,
              //           end: Alignment.bottomCenter),
              //       borderRadius: BorderRadius.circular(0.0)),
              //   child: new Column(
              //     crossAxisAlignment: CrossAxisAlignment.center,
              //     children: <Widget>[
              //       // Expanded(
              //       //   child: Padding(
              //       //     padding: EdgeInsets.all(10),
              //       //     child: Text('会员人数',
              //       //         style: TextStyle(
              //       //             color: white,
              //       //             fontSize: ScreenUtil.getInstance()
              //       //                 .setSp(NORMAL_FONT_SIZE))),
              //       //   ),
              //       // ),
              //       // Text(totalNum,
              //       //     style: TextStyle(
              //       //         color: white,
              //       //         fontSize: ScreenUtil.getInstance()
              //       //             .setSp(BIG_FONT_SIZE * 2))),
              //       new DefaultTabController(
              //         length: tabList.length,
              //         initialIndex: 0,
              //         child: new Column(
              //           children: <Widget>[
              //             TabBar(
              //               isScrollable: true,
              //               //是否可以滚动
              //               indicatorColor: white,
              //               controller: mController,
              //               labelColor: white,
              //               unselectedLabelColor: Color(0xffebebeb),
              //               labelStyle: TextStyle(
              //                   fontSize: ScreenUtil.getInstance()
              //                       .setSp(BIG_FONT_SIZE)),
              //               tabs: tabList.map((item) {
              //                 return Tab(
              //                   text: item,
              //                 );
              //               }).toList(),
              //             )
              //           ],
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
              Expanded(
                child: initList(),
              ),
              new Container(
                width: double.infinity,
                margin: EdgeInsets.all(30),
                decoration: new BoxDecoration(
                  color: mainColor,
                    borderRadius: BorderRadius.circular(
                        ScreenUtil.getInstance().setHeight(DP_60))),
                child: new FlatButton(
                  child: new Padding(
                    padding: EdgeInsets.all(0.0),
                    child: new Text(
                      "立即邀请",
                      style: new TextStyle(
                          color: Colors.white,
                          fontSize:
                              ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
                    ),
                  ),
                  onPressed: () {
                    // Navigator.of(context).push(MaterialPageRoute(builder:(context){
                    //     return new InviteFriendPage();
                    // }));
                  },
                ),
              ),
            ],
          ),
        ));
  }

  Widget initList() {
    return ListView.separated(
        itemBuilder: _renderRow,
        separatorBuilder: (BuildContext context, int index) {
          return Container(
            color: divideColor,
            height: 1,
          );
        },
        itemCount: list1 == null ? 0 : list1.length);
  }

  Widget _renderRow(BuildContext context, int index) {
    return new Container(
      color: Colors.white,
      padding: EdgeInsets.all(12.0),
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new Row(
            children: <Widget>[
              ClipOval(
                child: list1[index].avatar == ""
                    ? Image.asset(
                        'images/app_icon.png',
                        width: 50,
                        height: 50,
                        fit: BoxFit.fill,
                      )
                    : CachedNetworkImage(
                        imageUrl: list1[index].avatar,
                        width: 50,
                        height: 50,
                        fit: BoxFit.fill,
                      ),
              ),
              Expanded(
                flex: 2,
                child: new Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.fromLTRB(10, 5, 0, 0),
                      child: Text(
                        list1[index].name,
                        style: TextStyle(color: black, fontSize: 16),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(10, 5, 0, 2),
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            flex: 1,
                            child: Text(
                              list1[index].group_name,
                              style: TextStyle(color: black, fontSize: 14),
                            ),
                          ),
                          Text(
                            list1[index].register_time,
                            style: TextStyle(color: black, fontSize: 14),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
